/*
 * Copyright (c) 2018-2999 九五云信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 */

package com.yami.shop.multishop.controller;

import com.yami.shop.bean.app.param.SendEmailParam;
import com.yami.shop.bean.app.param.ShopUserRegisterParam;
import com.yami.shop.common.annotation.SysLog;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.util.PrincipalUtil;
import com.yami.shop.service.impl.VerificationCodeService;
import com.yami.shop.sys.comment.service.ShopUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 *
 *
 * @author lhd
 * @date 2020-07-04 09:54:16
 */
@RestController
@AllArgsConstructor
@RequestMapping("/shop/user")
@Api(tags = "商家用户接口")
public class ShopUserController {
    private final VerificationCodeService verificationCodeService;
    private final ShopUserService shopUserService;

    /**
     * 发送验证码到邮箱
     */
    @PostMapping("/sendEmailCode")
    @ApiOperation(value="发送邮箱注册验证码", notes="发送邮箱注册验证码")
    public ResponseEntity<Void> sendVerificationCode(@RequestBody SendEmailParam email) {
        verificationCodeService.sendVerificationCode(email.getEmail());
        return ResponseEntity.ok().build();
    }
    
//    /**
//     * 发送验证码到手机
//     */
//    @PostMapping("/sendMobileCode")
//    @ApiOperation(value="发送手机注册验证码", notes="发送手机注册验证码")
//    public ResponseEntity<Void> sendMobileVerificationCode(@RequestParam String mobile) {
//        if (!PrincipalUtil.isMobile(mobile)) {
//            throw new YamiShopBindException("yami.mobile.format.error");
//        }
//        verificationCodeService.sendVerificationCode(mobile);
//        return ResponseEntity.ok().build();
//    }
    
    /**
     * 商家用户注册
     * @param registerParam 注册参数
     * @return 注册结果
     */
    @SysLog("商家用户注册")
    @PostMapping("/register")
    @ApiOperation(value="商家用户注册", notes="商家用户注册")
    public ResponseEntity<String> register(@RequestBody @Valid ShopUserRegisterParam registerParam) {
        boolean result = shopUserService.register(registerParam);
        
        if (result) {
            return ResponseEntity.ok("yami.shop.user.register.success");
        } else {
            return ResponseEntity.badRequest().body("yami.shop.user.register.fail");
        }
    }
}
